package jp.ac.nitech.matlab.najm.server;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;



/**
 * <[  بسم الله الرحمان الرحيم  ]
 * @author k-hon
 * SNode: Data only server
 * 		
 * 	
 */

public class SNode {


    ServerSocket server = null;
    
  public static void main(String[] args) {
    SNode snode = new SNode();
    snode.snerve();
  }

  public void snerve() {
    int port = Integer.parseInt(SSetting.getInstance().getSetting("snode.port"));
    
    try {
    	server =  new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
    }
    catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }
    long time = System.currentTimeMillis();
    System.out.println("SNode():: Up and Running; start"+time+" @ "+server.getInetAddress()+":"+server.getLocalPort());
    // Loop waiting for a request
    while (true) {
      Socket socket = null;
      int handles=0;
      try {
        socket = server.accept();
        System.out.println("SNode():: Incoming Request @ "+(System.currentTimeMillis()-time));

        SNodeHandler service = new SNodeHandler(handles++, socket);
        Thread t = new Thread(service);
        t.start();
      }
      catch (Exception e) {
        e.printStackTrace();
        continue;
      }
    }
  }
}
